<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<div class="quiz-submission <%= "headless" if !@headers %>">
<% if !(@current_submission || @submission).results_visible?(user: @current_user)%>
  <h4><%= render_result_protection(@quiz, @current_submission || @submission) %></h4>
<% end %>

<% js_env IS_SURVEY: @quiz.survey? %>
<% unless show_correct_answers? %>
  <div class="alert">
    {{> icons/warning}}
    <span><%= render_correct_answer_protection(@quiz, (@current_submission || @submission)) %></span>
  </div>
<% end %>

<% if !(@current_submission || @submission) || (@current_submission || @submission).settings_only? %>
  <% if @quiz.survey? %>
    {{#t}}The survey hasn't been taken yet.{{/t}}
  <% else %>
    {{#t}}The quiz hasn't been taken yet.{{/t}}
  <% end %>
<% elsif !(@current_submission || @submission).results_visible?(user: @current_user) %>
  <div class="quiz_score" style="<%= hidden if @quiz.ungraded? %>">
    <% if @quiz.allowed_attempts != 1 %>
      <%= before_label(:score_for_attempt, "Score for this attempt") %>
    <% elsif @quiz.survey? %>
      <%= before_label(:score_for_survey, "Score for this survey") %>
    <% else %>
      <%= before_label(:score_for_quiz, "Score for this quiz") %>
    <% end %>
    <%= score_out_of_points_possible(@submission.score, params[:preview] ? @submission.points_possible_at_submission_time : @quiz.points_possible, :class => 'score_value') %>
    <% if (@current_submission || @submission).try(:pending_review?) %>
      *
      <div class='question-not-graded-text'>
        <% if can_do(@quiz, @current_user, :grade) && !@quiz.survey? %>
          <%= link_to t(:questions_not_graded, 'Some questions not yet graded'), context_url(@context, :context_quiz_history_url, @quiz, :version => @submission.version_number, :user_id => @submission.user_id) %>
        <% elsif !@quiz.survey? %>
          {{#t}}Some questions not yet graded{{/t}}
        <% end %>
      </div>
    <% end %>
  </div>
<% else %>
<% @stored_params ||= {}
    editable ||= false
    submission_data = @submission.data
    submission_data_hash = {}
    submission_data.each{|d| submission_data_hash[d[:question_id]] = d }
    submission_questions = @submission.questions_as_object
    submission_questions_hash = {}
    submission_questions.each{|q| submission_questions_hash[q[:id]] = q }
%>
<% if @submission.pending_review? %>
  <% if editable %>
    <% if @quiz.changed_significantly_since?(@submission.quiz_version) %>
      <%= image_tag "pending_review.png", :alt => '' %> {{#t}}The quiz has changed significantly since this submission was made. This submission may need re-grading.{{/t}}<br/>
    <% end %>
    <% questions_needing_review = submission_data.find_all do |answer|
         question_data = submission_questions_hash[answer[:question_id]]
         answer[:correct] == "undefined" && question_data && question_data[:question_type] != 'text_only_question'
       end %>
    <% if questions_needing_review.present? %>
      <div class="alert">
        {{> icons/warning}}
        <%= before_label(:questions_need_review, "The following questions need review") %>
        <ul id="questions_needing_review" style="margin-top: 0;">
          <% questions_needing_review.each do |answer| %>
            <% question = submission_questions_hash[answer[:question_id]] %>
            <li>
              <a href="#question_<%= answer[:question_id] %>"><%= h(question[:name]) %></a>
            </li>
          <% end %>
        </ul>
      </div>
    <% end %>
  <% end %>
<% end %>
<div class="quiz_score" style="<%= hidden if @quiz.ungraded? %>">
  <% if @quiz.allowed_attempts != 1 %>
    <%= before_label(:score_for_attempt, "Score for this attempt") %>
  <% elsif @quiz.survey? %>
    <%= before_label(:score_for_survey, "Score for this survey") %>
  <% else %>
    <%= before_label(:score_for_quiz, "Score for this quiz") %>
  <% end %>
  <%= score_out_of_points_possible(@submission.score, params[:preview] ? @submission.points_possible_at_submission_time : @quiz.points_possible, :class => 'score_value') %>
  <%= '*' if @submission.pending_review? %>
    </div>
<% if @submission.pending_review? %>
  <div class='question-not-graded-text'>
    *
    <% if can_do(@quiz, @current_user, :grade) && !@quiz.survey? %>
      <%= link_to t(:questions_not_graded, 'Some questions not yet graded'), context_url(@context, :context_quiz_history_url, @quiz, :version => @submission.version_number, :user_id => @submission.user_id) %>
    <% elsif !@quiz.survey?  %>
      {{#t}}Some questions not yet graded{{/t}}
    <% end %>
  </div>
<% end %>
<% if @submission.finished_at %>
<div>
  <%= t(:submitted_at, "Submitted %{when}", :when => datetime_string(@submission.finished_at)) %>
</div>
<% end %>
<div class="quiz_duration" style="<%= hidden if @quiz.ungraded? %>">
  <%= t(:attempt_duration, "This attempt took %{duration}.", :duration => duration_in_minutes(@submission.duration)) %>
</div>
<% if @quiz.require_lockdown_browser && @quiz.require_lockdown_browser_for_results && !@lockdown_browser_authorized_to_view && !can_do(@quiz, @current_user, :review_grades) %>
<p>
  <br /><br />
  {{#t}}Respondus LockDown Browser is required to view your quiz results.{{/t}}
  <%= link_to t('links.click_to_view_results', "Click here to view the results."), { :viewing => 1, :version => params[:version] }, :target => "_top" %>
</p>
<% else %>
<% css_bundle :quizzes %>
<% js_bundle 'legacy/quiz_submission' %>
<div id="questions" class="assessment_results <%= 'survey_results' if @quiz.survey? %> <%= (show_correct_answers? ? 'show_correct_answers' : 'suppress_correct_answers') %>">
  {{> views/display_question}}
</div>
<% if !@headers && editable && @current_user.preferences[:enable_speedgrader_grade_by_question] %>
  <div id="speed_update_scores_container">
<% else %>
  <div id="update_scores">
<% end %>

<% if editable %>
  <div class="update_scores_fudge">
    <label for="fudge_points_entry"><%= before_label(:fudge_points, "Fudge Points") %></label>
    <input type="hidden" name="fudge_points" value="<%= @submission.fudge_points || 0 %>">
    <input id="fudge_points_entry" type="number" step="any" name="fudge_points" value="<%= (!@submission.fudge_points || @submission.fudge_points == 0 ? '' : @submission.fudge_points) %>" placeholder="--" autocomplete='off' />
    <%
      fudge_text = begin
        if @submission.quiz_points_possible == @quiz.points_possible || !@submission.quiz_points_possible || !@quiz.points_possible
          t('labels.fudge_points_generic_explanation', "You can manually adjust the score by adding positive or negative points to this box.")
        elsif @quiz.survey?
          t('labels.fudge_points_changed_points_survey', <<-TEXT,
            Since %{user} took this survey, the number of points possible
            has changed by %{difference}. You can adjust for this
            change and manually add positive or negative points using
            this box.
            TEXT
            :user => @submission.user.name,
            :difference => @quiz.points_possible - @submission.quiz_points_possible)
        else
          t('labels.fudge_points_changed_points_quiz', <<-TEXT,
            Since %{user} took this quiz, the number of points possible
            has changed by %{difference}. You can adjust for this
            change and manually add positive or negative points using
            this box.
            TEXT
            :user => @submission.user.name,
            :difference => @quiz.points_possible - @submission.quiz_points_possible)
        end
      end
    %>

    <% if @current_user.preferences[:enable_speedgrader_grade_by_question] %>
      <span class="non-screenreader" aria-hidden=true>
        <i
          class="icon-question"
          data-tooltip='{"tooltipClass":"popover", "position":"bottom", "tooltipClass":"center bottom vertical"}'
          title="<%= fudge_text %>"></i>
      </span>
      <span class="screenreader-only"><%= fudge_text %></span>
    <% else %>
      <div><small><%= fudge_text %></small></div>
    <% end %>
  </div>
  <div class="update_scores">
    <div class="button-container">
      <button type="submit" class="btn btn-primary update-scores">{{#t}}Update Scores{{/t}}</button>
    </div>
  </div>
  <div class="update_scores_final_score">
    <b><%= before_label(:final_score, "Final Score") %></b>
      <%= score_out_of_points_possible(@submission.score || 0, @quiz.points_possible || 0, :id => 'after_fudge_points_total', :style => 'font-size: 1.5em; padding-left: 5px') %>
  </div>
<% end %>

<% if !editable %>
<div class="quiz_score" style="text-align: right; font-size: 1.2em; <%= hidden if @quiz.ungraded? %>">
  <% if @quiz.survey? %>
    <%= before_label(:survey_score, "Survey Score") %>
  <% else %>
    <%= before_label(:quiz_score, "Quiz Score") %>
  <% end %>
  <%= score_out_of_points_possible(@submission.score, params[:preview] ? @submission.points_possible_at_submission_time : @quiz.points_possible, :class => 'score_value') %>
  <% if @submission.fudge_points && @submission.fudge_points != 0 %>
    <div><small>
      <% if @quiz.survey? %>
        <% if @submission.fudge_points > 0 %>
          <%= t(:survey_fudged_positive, "This survey score has been manually adjusted by +%{fudge} points.", :fudge => @submission.fudge_points) %>
        <% else %>
          <%= t(:survey_fudged_negative, "This survey score has been manually adjusted by %{fudge} points.", :fudge => @submission.fudge_points) %>
        <% end %>
      <% else %>
        <% if @submission.fudge_points > 0 %>
          <%= t(:quiz_fudged_positive, "This quiz score has been manually adjusted by +%{fudge} points.", :fudge => @submission.fudge_points) %>
        <% else %>
          <%= t(:quiz_fudged_negative, "This quiz score has been manually adjusted by %{fudge} points.", :fudge => @submission.fudge_points) %>
        <% end %>
      <% end %>
    </small></div>
  <% end %>
  <% if @submission.pending_review? %>
    <div class='question-not-graded-text'>
      *
      <% if can_do(@quiz, @current_user, :grade) %>
        <%= link_to t(:questions_not_graded, 'Some questions not yet graded'), context_url(@context, :context_quiz_history_url, @quiz, :version => @submission.version_number, :user_id => @submission.user_id) %>
      <% else %>
        {{#t}}Some questions not yet graded{{/t}}
      <% end %>
    </div>
  <% end %>
</div>
<% end %>
</div> <!-- #speed_update_scores || #update_scores -->
<% end %>
<% end %>
</div>
